//
//  AppDelegate.m
//  AllPass
//
//  Created by apple on 2021/11/1.
//

#import "AppDelegate.h"
#import "DLDatabaseListVC.h"
#import "DLTabbarVC.h"
#import "BXNavigationController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    DLDatabaseListVC * tab = [[DLDatabaseListVC alloc]init];
    BXNavigationController * nav = [[BXNavigationController alloc]initWithRootViewController:tab];
    self.window.rootViewController = nav;
    return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    if([[url description] hasPrefix:@"file://"]){
        // Get the filename
        NSString *filename = [url lastPathComponent];
        // Get the full path of where we're going to move the file
        NSString *documentsDirectory = [Mytools documentsDirectory];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:filename];
        
        // Move input file into documents directory
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDirectory = NO;
        if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
            if (isDirectory) {
                // Should not have been passed a directory
                return NO;
            } else {
                [fileManager removeItemAtPath:path error:nil];
            }
        }
        [fileManager moveItemAtURL:url toURL:[NSURL fileURLWithPath:path] error:nil];
        
        // Set file protection on the new file
        [fileManager setAttributes:@{NSFileProtectionKey:NSFileProtectionComplete} ofItemAtPath:path error:nil];
        // Delete the Inbox folder if it exists
        [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:@"Inbox"] error:nil];
        [[NSNotificationCenter defaultCenter] postNotification:[[NSNotification alloc]initWithName:ReceiveDatabaseFromOtherApplication object:self userInfo:nil]];
    }
    return YES;
}
@end
